package com.bifrii.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.User;


public class JDBCEventDAO implements IEventDAO {

	@Override
	public List<Event> selectAllEvents() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Event> searchResults = new LinkedList<Event>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Event order by idEvent desc";
            stmt = conn.prepareStatement(sql);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Event temp = new Event();
                temp.setOid(result.getString("oid"));
                temp.setIdEvent(result.getInt("idEvent"));
                temp.setIdCommunity(result.getInt("idCommunity"));
                temp.setIdCreator(result.getInt("idCreator"));
                temp.setName(result.getString("name"));
                temp.setDescription(result.getString("description"));
                temp.setLimitAssistance(result.getInt("limitAssistance"));
                temp.setAvatar(result.getString("avatar"));
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}

	public List<User> selectAllUsers(Integer idEvent) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<User> searchResults = new LinkedList<User>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM User u, Assistance a where (a.idEvent = ? ) and a.idUser = u.idUser";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idEvent);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                User temp = new User();
                temp.setOid(result.getString("oid"));
                temp.setIdUser(result.getInt("idUser"));
                temp.setUser(result.getString("user"));
                temp.setPassword(result.getString("password"));
             
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}

	@Override

	public Event select(Integer idEvent) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "SELECT * FROM Event WHERE (idEvent = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Event e = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idEvent);

            result = stmt.executeQuery();

            result.next();
            e = new Event();
            e.setOid(result.getString("oid"));
            e.setIdEvent(result.getInt("idEvent"));
            e.setIdCommunity(result.getInt("idCommunity"));
            e.setIdCreator(result.getInt("idCreator"));
            e.setName(result.getString("Name"));
            e.setDescription(result.getString("description"));
            e.setLimitAssistance(result.getInt("limitAssistance"));
            e.setAvatar(result.getString("Avatar"));
          
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }

        return e;
	}

	@Override
	public void insert(Event e) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "INSERT INTO Event ( oid, idEvent, idCommunity, idCreator, name, description, avatar, limitAssistance) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, e.getOid());
            stmt.setInt(2, e.getIdEvent());
            stmt.setInt(3, e.getIdCommunity());
            stmt.setInt(4, e.getIdCreator());
            stmt.setString(5, e.getName());
            stmt.setString(6, e.getDescription());
            stmt.setString(7, e.getAvatar());
            stmt.setInt(8, e.getLimitAssistance());
            stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }

	}

	@Override
	public void delete(Integer idEvent) {
Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "DELETE FROM event WHERE (idEvent = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idEvent);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }

		
	}

	@Override
	public List<Event> selectTopEvents(Integer top) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Event> searchResults = new LinkedList<Event>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Event order by idEvent desc limit 0, ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, top);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Event temp = new Event();
                temp.setOid(result.getString("oid"));
                temp.setIdEvent(result.getInt("idEvent"));
                temp.setIdCommunity(result.getInt("idCommunity"));
                temp.setIdCreator(result.getInt("idCreator"));
                temp.setName(result.getString("name"));
                temp.setDescription(result.getString("description"));
                temp.setLimitAssistance(result.getInt("limitAssistance"));
                temp.setAvatar(result.getString("avatar"));
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}
		
}


